﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sispe.Entities;

namespace Sispe.Repository
{
    public class BancoRepository:MasterRepository, IBancoRepository
    {
        public List<BANCO> GetAllFromBanco()
        {
            var query = from b in context.BANCOes
                        select b;
            return query.ToList();
        }

        public List<BANCO> GetAllFromBancoByDescripcion(string descripcion)
        {
            var query = from b in context.BANCOes
                        select b;
            if (descripcion != null && descripcion != string.Empty && descripcion != "")
            {
                query = from b in context.BANCOes
                        where b.Descripcion.ToUpper().Contains(descripcion)
                        select b;
            }
            return query.ToList();
        }

        public bool ExisteDuplicidad(string descripcion, int id)
        {
            var query = from b in context.BANCOes
                        where b.Descripcion.ToUpper().CompareTo(descripcion.ToUpper()) == 0 && b.IdBanco != id
                        select b;
            return query.Any();
        }

        public void AddBanco(BANCO banco)
        {
            context.BANCOes.Add(banco);
            context.SaveChanges();
        }

        public void UpdateBanco(BANCO banco)
        {
            context.Entry(banco).State = System.Data.EntityState.Modified;
            context.SaveChanges();
        }

        public void RemoveBanco(int id)
        {
            var existe = context.BANCOes.Find(id);

            if (existe != null)
            {
                context.BANCOes.Remove(existe);
                context.SaveChanges();
            }
        }

        public BANCO GetFromBancoById(int id)
        {
            var query = from b in context.BANCOes
                        where b.IdBanco.Equals(id)
                        select b;
            return query.SingleOrDefault();
        }
    }
}
